<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class PhotoController extends Controller
{
     public function index()
    {
    	$userid = session('homeuser')->userid;
    	$list = DB::table('cloud')->whereNotNull('pname')->where('userid',$userid)->get();
    	return view('home.cloud.photo',['list'=>$list]);
    }
    public function destroy($id)
    {
    	DB::table('cloud')->where('cloudid',$id)->delete();
    	return redirect('home/photo');
    }
    public function create()
    {
    	$userid = session('homeuser')->userid;
    	return view('home.cloud.padd',['userid'=>$userid]);
    }
    public function store(Request $request)
    {
    	if ($request->hasFile('pic')) {
            if ($request->file('pic')->isValid()) {
                $file = $request->file('pic');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $picname = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./cloud/',$picname);
                //自定义类进行图片缩放处理
                // $img = new Image();
                // $im = $img->open('./uploads/'.$picname);
                // $im->thumb(160,110)->save('./uploadss/'.$picname);
                $userid = $request->input('userid');
                if ($request->has('pname')) {
                	$pname = $request->input('pname');
	                $data = array('userid'=>$userid,'pname'=>$pname,'photo'=>$picname);
	                $m = DB::table('cloud')->insertGetId($data);
	                if ($m>0) {
	                	return redirect('home/photo');
	                }else{
	        			return back()->with('msg','上传失败,图片格式或数量不正确');
	        		}
                }else{
                	return back()->with('msg','上传失败,名字不能为空');
                }
            }
        }
    }
    public function edit($id)
    {
    	$list = DB::table('cloud')->where('cloudid',$id)->first();
    	return view('home.cloud.pedit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
    	$pname = $request->input('pname');
        $data = array('pname'=>$pname);
        $m = DB::table('cloud')->where('cloudid',$id)->update($data);
        return redirect('home/photo');
    }
}
